function [vol info] = GetCTtiffImage(Dir)
% Load stack of TIFF images and return the 3D volume and information
% extracted from the first image.
%
% Usage: 
%    >> [img ~] = GetCTtiffImage('./Cache/1008.2.18.7.1.1.2');
%    >> imagesc3(img);  % View the 3D image
%
%  - Michael Eager (michael.eager@monash.edu)

%     Copyright © 2012-2013 Michael Eager <michael.eager@monash.edu> 
%
%     This file is part of Xglom.
% 
%     This is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/>.


disp('GetCTtiffImage called')
%hMesg = msgbox('Please wait while CT Tiff stack is being loaded.', 'Xglom Loading CT image');
addpath(fullfile(pwd,'plugins','progressbar'));

gui_active(1);
hProgress  = progressbar([],0,'Please wait while CT TIFF stack is being loaded.' );
d = dir(Dir);

dirnames = sort({d([d.isdir]).name});
dirnames(ismember(dirnames, {'.', '..'})) = '';

%[status Zdirs] = system(['ls -1 ' Dir '| grep -e ''*_01_01'' | sort -n']);
%Zdirs =regexp(Zdirs, '\n', 'split');                 % Split the output into a list
%Zdirs(cellfun(@isempty, Zdirs))=[];                  % Clean the list

% exclude txt and mat files based on duploadtiff format
%Zdirs( find(cellfun(@isempty,regexp(Zdirs, '[0-9]{4}_01_01')))) = [];


info = imfinfo(fullfile(Dir,dirnames{1},'0001.tif'));

if exist(fullfile(Dir,'info.txt'),'file')
    data_cell_string = fileread(fullfile(Dir,'info.txt'));
    data_cell_string = regexp(data_cell_string,'\s+','split');
    
    for i=1:length(data_cell_string)
        if ~isempty(strfind(data_cell_string{i},'NZ='))
            info.NumSlides =  str2double(data_cell_string{i}(4:end));
        end
        if ~isempty(strfind(data_cell_string{i},'DX='))
            info.PixelResolution =  str2double(data_cell_string{i}(4:end));
        end
        if ~isempty(strfind(data_cell_string{i},'DZ='))
            info.SliceSeperation =  str2double(data_cell_string{i}(4:end));
        end
        if ~isempty(strfind(data_cell_string{i},'TX='))
            info.XTiles =  str2double(data_cell_string{i}(4:end));
        end
        if ~isempty(strfind(data_cell_string{i},'TY='))
            info.YTiles =  str2double(data_cell_string{i}(4:end));
        end
    end
end

switch info.BitDepth
case 8
vol = zeros(info.Width,info.Height,numel(dirnames),'uint8');
case 16 
vol = zeros(info.Width,info.Height,numel(dirnames),'uint16');
otherwise
vol = zeros(info.Width,info.Height,numel(dirnames),'single');
end


prog_incr=1.0/double(numel(dirnames))
for ii = 1:numel(dirnames)
    
    vol(:,:,ii) = imread(fullfile(Dir, dirnames{ii},'0001.tif'));
    hProgress = progressbar( hProgress, prog_incr,'Please wait while CT DICOM is being loaded.' );
    
end
progressbar( hProgress, -1 );
if ~hProgress
    disp('Progressbar removed.');
end
%delete(hMesg)
